(function() {
	angular
		.module('ydApp.home', [])
		.config(function($stateProvider) {
			$stateProvider.state('tabs.home', {
				// url: '/home',
				// cache: false,
				// controller: "homeCtrl",
				// templateUrl: 'views/home/home.html'
				url: '/home',
				nativeTransitions: null,
				views: {
					'home-tab': {
						controller: "homeCtrl",
						templateUrl: 'views/home/home.html'
					}
				}
			});
		})
		.controller("homeCtrl", homeCtrl);

	function homeCtrl($scope, $timeout, homeService, $rootScope, popup, $cordovaInAppBrowser, localStore, $location, $state, xhr, $ionicSideMenuDelegate) {
		var vm = $scope.vm = {};

		var user = localStore.get('user');

		vm.getMsgCount = function() {
			xhr.get('HealthInquiry/com/sysMessage/countUnreadByUserId', {
				userId: user.id
			}).then(function(result) {
				vm.count = result.data.count;
			})
		}

		vm.getMsgCount();

		$scope.toggleSlide = function() {
			vm.getMsgCount();
			getMessage();
			$scope.slideRight = !$scope.slideRight;
			vm.getMsgCount();
		}
		$scope.swipeLeft = function(e) {
			vm.getMsgCount();
			getMessage();
			$scope.slideRight = true;
		}
		$scope.swipeRight = function(e) {
			vm.getMsgCount();
			getMessage();
			$scope.slideRight = false;
		}

		//每分钟获取一次消息数目
		var getMsgCountEveryminute = function() {
			homeService.preventCycle = true;
			$timeout(function() {
				vm.getMsgCount();
				getMsgCountEveryminute();
			}, 60000)
		};

		if (!homeService.preventCycle) {
			getMsgCountEveryminute();
		}

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		$scope.search = function(event) {
			if (!vm.search) {
				return;
			}
			if (event.keyCode == 13) {
				$state.go('doc-list', {
					keyword: vm.search
				})
			}
		};
		xhr.get('HealthInquiry/com/advertising/findByPosition', {
			position: 1
		}).then(function(result) {
			vm.topBanners = result.data;
			//vm.banners = ['img/temp-banner.jpg'];
			for (var i = 0; i < vm.topBanners.length; i++) {
				vm.topBanners[i].imgUrl = $scope.downloadImg(vm.topBanners[i].imgUrl);
			}
			vm.topCarouselIndex = 0;
			if (vm.topBanners.length > 0) {
				var img = new Image();
				img.onload = function() {
					$scope.$apply(function() {
						var height = (window.screen.width / img.width) * img.height;
						vm.height = height;
					})
				}
				img.src = $scope.downloadImg(vm.topBanners[0].imgUrl);
			}
		})

		xhr.get('HealthInquiry/com/advertising/findByPosition', {
			position: 2
		}).then(function(result) {
			vm.middleBanners = result.data;
			//vm.banners = ['img/temp-banner.jpg'];
			for (var i = 0; i < vm.middleBanners.length; i++) {
				vm.middleBanners[i].imgUrl = $scope.downloadImg(vm.middleBanners[i].imgUrl);
			}
			vm.middleCarouselIndex = 0;
			if (vm.middleBanners.length > 0) {
				var img = new Image();
				img.onload = function() {
					$scope.$apply(function() {
						var height = (window.screen.width / img.width) * img.height;
						vm.height = height;
					})
				}
				img.src = vm.middleBanners[0].imgUrl;
			}
		})

		var getMessage = function() {
			return xhr.get('HealthInquiry/com/sysMessage/findByCondition', {
				userId: user.id
			}).then(function(result) {
				vm.message = result.data;
				for (var i = 0; i < vm.message.length; i++) {
					s1 = new Date(vm.message[i].sendTimeString.replace(/-/g, "/"));
					var now = new Date();
					s2 = new Date(now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' 23:59:59');
					var days = s2.getTime() - s1.getTime();
					var time = parseInt(days / (1000 * 60 * 60 * 24));
					if (time == 0) {
						vm.message[i].day = "今天";
					} else if (time == 1) {
						vm.message[i].day = "昨天";
					} else {
						vm.message[i].day = "昨天之前";
					}
				}
			})
		};
		vm.doRefresh = function () {
			console.log('asd')
			getMessage().then(function () {
				vm.getMsgCount();
				$scope.$broadcast('scroll.refreshComplete');
			});
		}

		getMessage();

		xhr.get('HealthInquiry/com/article/findByCondition', {
			pageIndex: 0,
			pageSize: 5
		}).then(function(result) {
			vm.health = result.data;
		})

		vm.showMsg = function() {
			$ionicSideMenuDelegate.toggleRight();
		}
		vm.showSetting = function(argument) {
			vm.msgSetting = !vm.msgSetting;
		}
		vm.doRead = function() {
			xhr.get('HealthInquiry/com/sysMessage/readAll', {
				userId: user.id
			}).then(function() {
				popup.alert("标记成功！");
				vm.showSetting();
				vm.getMsgCount();
				getMessage();
			});
		}
		vm.deleteAll = function() {
			xhr.get('HealthInquiry/com/sysMessage/deleteAll', {
				userId: user.id
			}).then(function() {
				popup.alert("清除成功！");
				vm.getMsgCount();
				vm.showSetting();
				getMessage();
			});
		}

		var options = {
			location: 'no',
			clearcache: 'no',
			toolbar: 'yes'
		};

		$scope.open = function(link) {
			$cordovaInAppBrowser.open(link, '_blank', options);
		}

		$scope.showMessage = function(message) {
			xhr.get("HealthInquiry/com/sysMessage/readById", {
				msgId: message.id,
				userId: user.id
			}).then(function(result) {
				vm.count -= 1;
				message.state = 1;
				$state.go("messageDetail", {
					id: message.id,
					sendUserId: message.sendUserId,
					content: message.content,
					sendTimeString: message.sendTimeString
				})
			});
		}

	}
})();

(function() {
	'use strict';
	angular.module('ydApp.home').factory('homeService', [function() {
		return {
			preventCycle: false
		}
	}])
})();